//Set I2C Start condition
void I2C_Start()
{
	TWCR =0xA4;										//START condition
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) != 0x08));	//START complete?
}
//Set I2C REAPEATED START condition
void I2C_ReStart()
{
	TWCR = 0xA4;									//REAPEATED START condition
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) != 0x10));	//START complete?
}
//Set I2C Stop condition
void I2C_Stop()
{
	TWCR = 0x94;
}

//ADS_7828_READ single ch (1ch) read
unsigned int ADS7828_READ_L()
{
	unsigned int adcvalue=0;
	I2C_Start();

	TWDR = 0x90;								//SLA + W
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x18));	//SLA + W complete? 

	TWDR = 0x8C;								//Command Byte
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x28));	//Command complete?

	I2C_ReStart();

	TWDR = 0x91;								//SLA + R
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x40));	//SLA + R complete? 

	TWCR = 0xC4;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x50));	//data high complete?
	adcvalue = TWDR * 256;

	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x58));	//data high complete?
	adcvalue += TWDR;

	I2C_Stop();

	return adcvalue;
}

//ADS_7828_READ_ch ADS7828 ch read
unsigned int ADS7828_READ_R()
{
	unsigned int adcvalue=0;
	I2C_Start();

	TWDR = 0x90;								//SLA + W
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x18));	//SLA + W complete? 

	TWDR = 0x9C;												//Command Byte
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x28));	//Command complete?

	I2C_ReStart();

	TWDR = 0x91;								//SLA + R
	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x40));	//SLA + R complete? 

	TWCR = 0xC4;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x50));	//data high complete?
	adcvalue = TWDR * 256;

	TWCR = 0x84;
	while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xF8) !=  0x58));	//data high complete?
	adcvalue += TWDR;

	I2C_Stop();

	return adcvalue;
}